package com.zzs.elm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zzs.elm.po.Business;
import com.zzs.elm.po.Orders;
import com.zzs.elm.util.DBUtil;

public class OrdersDao {

	public OrdersDao() {
		super();
	}

	public OrdersDao(Connection con) {
		super();
		this.con = con;
	}
	
	private Connection con;
	private PreparedStatement pst = null;
	private ResultSet rs = null;
	
	public int saveOrders(Orders orders) throws Exception {
		int result = 0;
		String sql = "insert into orders values(null, ?, ?, now(), ?, ?, 0)";
		try {
			pst = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
			pst.setString(1, orders.getUserId());
			pst.setInt(2, orders.getBusinessId());
			pst.setDouble(3, orders.getOrderTotal());
			pst.setInt(4, orders.getDeliveryAddressId());
			pst.executeUpdate();
			rs = pst.getGeneratedKeys();
			if(rs.next()) {
				result = rs.getInt(1);
			}
			return result;
		} finally {
			DBUtil.close(rs, pst);
		}
	}
	
	// TODO points
	public Orders getOrdersById(Integer orderId) throws Exception {
		Orders order = null;
		String sql = "select o.*, businessName, deliveryPrice from orders o , business b where orderId = ? and o.businessId = b.businessId";
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, orderId);
			rs = pst.executeQuery();
			if (rs.next()) {
				order = new Orders();
				order.setOrderId(rs.getInt("orderId"));
				order.setUserId(rs.getString("userId"));
				order.setBusinessId(rs.getInt("businessId"));
				order.setOrderDate(rs.getString("orderDate"));
				order.setOrderTotal(rs.getDouble("orderTotal"));
				order.setDeliveryAddressId(rs.getInt("daId"));
				order.setOrderState(rs.getInt("orderState"));
				
				Business business = new Business();
				business.setBusinessId(rs.getInt("businessId"));
				business.setBusinessName(rs.getString("businessName"));
				business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
				order.setBusiness(business);
			}
			return order;
		} finally {
			DBUtil.close(rs, pst);
		}
	}
	
	public List<Orders> listOrdersByUserId(String userId) throws Exception {
		if (userId == null)
			throw new NullPointerException();
		List<Orders> list = new ArrayList<>();
		String sql = "select o.*, businessName, deliveryPrice from orders o left join business b on o.businessId = b.businessId where userId = ?";
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, userId);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				Orders orders = new Orders();
				orders.setOrderId(rs.getInt("orderId"));
				orders.setUserId(rs.getString("userId"));
				orders.setBusinessId(rs.getInt("businessId"));
				orders.setOrderDate(rs.getString("orderDate"));
				orders.setOrderTotal(rs.getDouble("orderTotal"));
				orders.setDeliveryAddressId(rs.getInt("daId"));
				orders.setOrderState(rs.getInt("orderState"));
				Business business = new Business();
				business.setBusinessId(orders.getBusinessId());
				business.setBusinessName(rs.getString("businessName"));
				business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
				orders.setBusiness(business);
				list.add(orders);
			}
			return list;
		} finally {
			DBUtil.close(rs, pst);
		}
	}

	public int payOrder(String userId, Integer orderId) throws Exception {
		String sql = "update orders set orderState=1 where orderId=? and userId=?";
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, orderId);
			pst.setString(2, userId);
			return pst.executeUpdate();
		} finally {
			DBUtil.close(rs, pst, con);
		}
	}
	
}